/*
 * InvulgoTracker
 * Copyright (C) Thomas Andersen 2011 <thomas.1.andersen@gmail.com>
 *
 * InvulgoTracker is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * InvulgoTracker is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <libxml/xmlwriter.h>
#include <libxml/encoding.h>
#include "recenttasks.h"
#include "config.h"
#include "util.h"

#define MY_ENCODING "ISO-8859-1"

RecentTasks::RecentTasks()
{
    Load();
}

RecentTasks::~RecentTasks()
{
}

void RecentTasks::Add(TaskId id)
{
    recent.remove(id);
    recent.push_front(id);
    while(recent.size() > RECENTTASKCOUNT)
        recent.pop_back();
}

void RecentTasks::AddBack(TaskId id)
{
    recent.push_back(id);
}

bool RecentTasks::GetFirst(TaskId &id)
{
    it = recent.begin();

    if (it != recent.end()) {
        id = *it;
        return true;
    }
    return false;
}

bool RecentTasks::GetNext(TaskId &id)
{
    ++it;

    if (it != recent.end()) {
        id = *it;
        return true;
    }
    return false;
}



void RecentTasks::Load()
{
    xmlDocPtr doc;
    xmlChar *key;
    char *filename = new char[PATH_MAX+1];

    sprintf(filename, "%s%srecent.xml",g_get_home_dir(),"/.InvulgoTracker/");

    if (!access(filename, F_OK)) {
        doc = xmlParseFile(filename);

        if (doc) {
            xmlNodePtr cur;
            cur = xmlDocGetRootElement(doc);
            if (!xmlStrcmp(cur->name, (const xmlChar *) "RecentTasks")) {
                cur = cur->xmlChildrenNode;
                while (cur != NULL) {
                    if ((!xmlStrcmp(cur->name, (const xmlChar *)"Task"))) {
                        key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
                        AddBack(TaskId((gchar *)key));
                        xmlFree(key);
                    }
                    cur = cur->next;
                }
            }
            xmlFreeDoc(doc);
        }
    }
    delete[] filename;
}


void RecentTasks::Save()
{
    int rc;
    xmlTextWriterPtr writer;
    xmlDocPtr doc;
    xmlChar *tmp;

    /* Create a new XmlWriter for DOM, with no compression. */
    writer = xmlNewTextWriterDoc(&doc, 0);
    if (writer == NULL) return;

    rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
    if (rc < 0) return;

    rc = xmlTextWriterStartElement(writer, BAD_CAST "RecentTasks");
    if (rc < 0) return;

    tmp = ConvertInput("Recent tasks for InvulgoTracker. See www.invulgo.dk for details.", MY_ENCODING);
    rc = xmlTextWriterWriteComment(writer, tmp);
    if (tmp != NULL) xmlFree(tmp);
    if (rc < 0) return;

    /* Save Recent tasks. */
    TaskId ti;
    if (GetFirst(ti)) {
        do {
            xmlTextWriterWriteFormatElement(writer, BAD_CAST "Task","%s", (const gchar *)ti);
        } while(GetNext(ti));
    }

    /* Close xml. */
    rc = xmlTextWriterEndElement(writer);
    if (rc < 0) return;

    rc = xmlTextWriterEndDocument(writer);
    if (rc < 0) return;

    xmlFreeTextWriter(writer);

    char *filename = new char[PATH_MAX+1];

    sprintf(filename, "%s%srecent.xml",g_get_home_dir(),"/.InvulgoTracker/");

    xmlSaveFormatFile(filename, doc, 1);

    delete[] filename;

    xmlFreeDoc(doc);

}
